#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/2/4 16:51
# @Author  : 王凯
# @File    : config_spider.py
# @Project : scrapy_spider
from typing import List, Dict, Optional, Union
from pydantic import BaseModel
import yaml


class CustomField(BaseModel):
    name: Optional[str] = ''
    css: Optional[str] = ''
    xpath: Optional[str] = ''
    attr: Optional[str] = ''
    re_first: Optional[str] = ''
    next_stage: Optional[str] = ''
    remark: Optional[str] = ''


class Stage(BaseModel):
    name: Optional[str] = ''
    is_list: bool = ''
    list_css: Optional[str] = ''
    list_xpath: Optional[str] = ''
    page_css: Optional[str] = ''
    page_xpath: Optional[str] = ''
    page_attr: Optional[str] = ''
    page_loop_xpath: Optional[str] = ''
    page_loop_css: Optional[str] = ''
    page_loop_attr: Optional[str] = ''
    page_loop_page_size: Union[Optional[int], Optional[str]] = ''
    page_loop_re_first: Optional[str] = ''
    page_loop_sub_src: Optional[str] = ''
    page_loop_sub_target: Optional[str] = ''
    fields: List[CustomField] = ''


class ConfigSpiderData(BaseModel):
    name: Optional[str] = ''
    display_name: Optional[str] = ''
    static_self_item_key: Optional[list] = []
    province: Optional[str] = ''
    city: Optional[str] = ''
    county: Optional[str] = ''
    park: Optional[str] = ''
    source: Optional[str] = ''
    url: Optional[str] = ''
    col: Optional[str] = ''
    remark: Optional[str] = ''
    type: Optional[str] = ''
    engine: Optional[str] = ''
    start_url: Optional[str] = ''
    start_stage: Optional[str] = ''
    stages: List[Stage] = ''
    settings: Union[None, Dict[str, Union[str, bool, dict]]] = {}
    cmd: Optional[str] = ''


class Constants:
    AnchorStartStage: str = "START_STAGE"
    AnchorStartUrl: str = "START_URL"
    AnchorParsers: str = "PARSERS"
    AnchorName: str = "NAME"
    NAME_NO_LINE: str = "NAME_NO_LINE"
    CUSTOM_SETTINGS: str = "CUSTOM_SETTINGS"
    STATIC_SELF_ITEM: str = "STATIC_SELF_ITEM"


if __name__ == '__main__':
    with open("example.yaml", "r", encoding='utf-8') as file:
        data = yaml.safe_load(file)
        data = ConfigSpiderData(**data)
    data.stages = []
    print(data)
